<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="账号" prop="leUsername">
        <el-input
          v-model="queryParams.leUsername"
          placeholder="请输入账号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="leNumber">
        <el-input
          v-model="queryParams.leNumber"
          placeholder="请输入手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['applet:league:add']"
          >新增</el-button
        >
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-table
      v-loading="loading"
      :data="leagueList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="index" label="序号" align="center" />
      <el-table-column label="账号" align="center" prop="leUsername" />
      <el-table-column label="密码" align="center" prop="lePassword" />
      <el-table-column label="授权店" align="center" prop="leShop" />
      <el-table-column label="授权编码" align="center" prop="leCode" />
      <el-table-column label="门店图片" align="center" prop="lePic">
        <template slot-scope="{ row }">
          <img :src="getImage(row.lePic)" alt="" style="height: 100px" />
        </template>
      </el-table-column>
      <el-table-column label="负责人姓名" align="center" prop="leName" />
      <el-table-column label="手机号" align="center" prop="leNumber" />
      <el-table-column label="状态" align="center" prop="leStatus" />
      <el-table-column label="排序" align="center" prop="leOrder" />
      <el-table-column label="经纬度" align="center" prop="leTrapeze" />
      <el-table-column label="地址" align="center" prop="leSite" />
      <el-table-column label="审批时间" align="center" prop="leTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
         <template slot-scope="scope">
           <el-button
             size="mini"
             type="text"
             icon="el-icon-edit"
             @click="handleUpdate(scope.row)"
             v-hasPermi="['applet:league:edit']"
           >修改</el-button>
           <el-button
             size="mini"
             type="text"
             icon="el-icon-delete"
             @click="handleDelete(scope.row)"
             v-hasPermi="['applet:league:remove']"
           >删除</el-button>
         </template>
       </el-table-column>
     </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 修改加盟商对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-form-item label="账号" prop="leUsername">
          <el-input v-model="form.leUsername" placeholder="请输入账号" :disabled="ifedit"/>
        </el-form-item>
        <el-form-item label="密码" prop="lePassword">
          <el-input v-model="form.lePassword" placeholder="请输入密码" />
        </el-form-item>
        <el-form-item label="授权店" prop="leShop">
          <el-input v-model="form.leShop" placeholder="请输入授权店" />
        </el-form-item>
        <el-form-item label="授权编码" prop="leCode">
          <el-input v-model="form.leCode" placeholder="请输入授权编码" />
        </el-form-item>
        <el-form-item label="门店图片" prop="lePic">
          <el-upload
            class="avatar-uploader"
            :action="uploadFileUrl"
            :show-file-list="false"
            :headers="headers"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="image" :src="image" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="负责人姓名" prop="leName">
          <el-input v-model="form.leName" placeholder="请输入负责人姓名" />
        </el-form-item>
        <el-form-item label="手机号" prop="leNumber">
          <el-input v-model="form.leNumber" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="排序" prop="leOrder">
          <el-input-number
            v-model="form.leOrder"
            :min="1"
            :max="1000"
            label="排序"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="经纬度" prop="leTrapeze">
          <el-input v-model="form.leTrapeze" placeholder="请输入经纬度" />
          <a class="colour" href="https://lbs.qq.com/getPoint/" target="_blank"
            >拾取坐标</a
          >
        </el-form-item>
        <el-form-item label="状态" prop="leStatus">
          <el-radio-group v-model="form.leStatus">
            <el-radio :label="0">禁用</el-radio>
            <el-radio :label="1">启用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="地址" prop="leSite">
          <el-input v-model="form.leSite" placeholder="请输入地址" />
        </el-form-item>
        <el-form-item label="详细地址" prop="leLocation">
          <el-input v-model="form.leLocation" placeholder="请输入详细地址" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listLeague,
  getLeague,
  delLeague,
  addLeague,
  updateLeague,
} from "@/api/applet/league";
import { getToken } from "@/utils/auth";

export default {
  name: "league",
  data() {
    return {
      ifedit:true,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 加盟商表格数据
      leagueList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        leUsername: null,
        lePassword: null,
        leShop: null,
        leCode: null,
        lePic: null,
        leName: null,
        leNumber: null,
        leStatus: null,
        leOrder: null,
        leTrapeze: null,
        leSite: null,
        leLocation: null,
        creationTime: null,
        leTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        leStatus: [
          {
            required: true,
            message: "状态 0禁用 1启用 2删除不能为空",
            trigger: "change",
          },
        ],
      },
      UrlServer: process.env.VUE_APP_BASE_API,
      uploadFileUrl: process.env.VUE_APP_BASE_API + "/common/upload", // 上传文件服务器地址
      headers: {
        Authorization: "Bearer " + getToken(),
      },
      image: "",
      toApplet: "", //登录名 userName
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //图片回显
    getImage(url) {
      return this.UrlServer + url;
    },
    /** 查询加盟商列表 */
    getList() {
      this.loading = true;
      listLeague(this.queryParams).then((response) => {
        response.rows.forEach((item) => {
          item.leStatus = item.leStatus == 1 ? "启用" : "禁用";
        });
        this.leagueList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        leUsername: null,
        lePassword: null,
        leShop: null,
        leCode: null,
        lePic: null,
        leName: null,
        leNumber: null,
        leStatus: null,
        leOrder: null,
        leTrapeze: null,
        leSite: null,
        leLocation: null,
        creationTime: null,
        leTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.ifedit=false;
      this.reset();
      this.open = true;
      this.title = "添加加盟商";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.ifedit=true;
      this.reset();
      const id = row.id || this.ids;
      getLeague(id).then((response) => {
        this.form = response.data;
        this.image = this.UrlServer + this.form.lePic;
        this.open = true;
        this.title = "修改加盟商";
      });
    },
    // 上传图片
    handleAvatarSuccess(res, file) {
      this.image = URL.createObjectURL(file.raw);
      this.form.lePic = res.fileName;
    },
    beforeAvatarUpload(file) {
      const isLt2M = file.size / 1024 / 1024 < 200;
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 200MB!");
      }
      return isLt2M;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateLeague(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addLeague(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除加盟商编号为"' + ids + '"的数据项？')
        .then(function () {
          return delLeague(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "applet/league/export",
        {
          ...this.queryParams,
        },
        `league_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.colour {
  color: rgb(206, 77, 68); /* 任何有效的CSS颜色值 */
}
</style>
